#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define    logInfo(xx,args...)    fprintf(stderr, xx"\n", ##args)
#define    logError(xx,args...)    fprintf(stderr, xx"\n", ##args)

void parseStr(char *buf)
{
    int len = strlen(buf);
    int count = 0;
    int lastIndex = 0;
    int agvId;
    int speedMax;
    int digits1[4];
    int digits2[4];
    memset(digits1, 0, sizeof(int) * 4);
    memset(digits2, 0, sizeof(int) * 4);
    int writeIndex = 0;
    int i;
    for (i = 0; i <= len;i++)
    {
        if ((buf[i] == ',') || ('\0' == buf[i]))
        {
            count++;
            if (3 == count)
            {
               // buf[i] = '\0';
                sscanf(buf, "%d,%d", &agvId, &speedMax);
                lastIndex = i + 1;
            }
            else if (count < 3)
            {
                ;
            }
            else if (0 == (count - 3) % 2)
            {
              //  buf[i] = '\0';
                sscanf(buf + lastIndex, "%d,%d", &digits1[writeIndex], &digits2[writeIndex]);
                lastIndex = i + 1;
                writeIndex++;
                logInfo("writeIndex=%d", writeIndex);
                if (writeIndex >= 4)
                {
                    break;
                }
            }
        }
    }
    logInfo("[%d,%d,%d,%d]", digits1[0], digits1[1], digits1[2], digits1[3]);
    logInfo("[%d,%d,%d,%d]", digits2[0], digits2[1], digits2[2], digits2[3]);
}

int main()
{
    logInfo("Hello World!\n");
  //  char strbuf[1024] = "1,2";
    char strbuf[1024] = "12,13,14,1,2,3,4,5,6,7,8";
   // char strbuf[1024] = "1 2 3 4 5 6 7 8";
    int digits1[4];
    int digits2[4];
    memset(digits1, 0, sizeof(int) * 4);
    memset(digits2, 0, sizeof(int) * 4);

    int ret;
     ret = sscanf(strbuf,  "%d,%d", &digits1[0], &digits2[0]);
     ret = sscanf(strbuf,  "%d,%d", &digits1[1], &digits2[1]);
    logInfo("ret=%d", ret);
    logInfo("[%d,%d,%d,%d]", digits1[0], digits1[1], digits1[2], digits1[3]);
    logInfo("[%d,%d,%d,%d]", digits2[0], digits2[1], digits2[2], digits2[3]);
    parseStr(strbuf);

    return 0;
}
